package com.huarui.purchaseOrder.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;


import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
import com.huarui.purchaseOrder.service.PurchaseOrderService;
import com.huarui.util.FuncUtil;

/**
 * 
 * @author ycf
 *
 */
public class PurchaseAddAction extends BaseAction{

	public static final String MESS = "mess";
	
	private static final long serialVersionUID = -6433874154322733457L;
	private Logger logger = Logger.getLogger(PurchaseAddAction.class);

	private PurchaseOrderService purchaseOrderService;
	
	/**
	 * 主表信息
	 */
	private String purOrder;
	
	/**
	 * 明细信息集合
	 */
	private String purDetails;
	
	
	/**
	 * 返回到页面信息
	 */
	private Map<String, Object> mesMap;
	

	public String getPurOrder() {
		return purOrder;
	}


	public void setPurOrder(String purOrder) {
		this.purOrder = purOrder;
	}


	public String getPurDetails() {
		return purDetails;
	}


	public void setPurDetails(String purDetails) {
		this.purDetails = purDetails;
	}


	public Map<String, Object> getMesMap() {
		return mesMap;
	}


	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}


	public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	/**
	 * 主订单信息
	 * @return
	 */
	private PurOrSaleOrder getPurOrSaleOrder() {
		JSONObject jo_purOrder = JSONObject.fromObject(purOrder);
		FuncUtil.removeNullValue(jo_purOrder);
		PurOrSaleOrder orders = (PurOrSaleOrder) JSONObject.toBean(jo_purOrder,
				PurOrSaleOrder.class);
		return orders;
	}

	/**
	 * 明细订单信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<PurOrSaleOrderDetail> getDetailsList() {
		JSONArray ja_purDetails = JSONArray.fromObject(purDetails);
		List<PurOrSaleOrderDetail> ps = (List<PurOrSaleOrderDetail>) JSONArray
				.toCollection(ja_purDetails, PurOrSaleOrderDetail.class);
		return ps;
	}
	
	private String insertPurAndDetails() {
		
		PurOrSaleOrder orders = this.getPurOrSaleOrder();
		
		List<PurOrSaleOrderDetail> ps = this.getDetailsList();

		mesMap = purchaseOrderService.insertPurAndDetails(orders, ps);
		
		return MESS;
	}
	
	/**
	 * 保存在线采购订单
	 * @return
	 */
	public String saveOnlinePurAndDetails() {
		return this.insertPurAndDetails();
	}
	
	/**
	 * 保存非在线采购订单
	 * @return
	 */
	public String saveNotOnlinePurAndDetails() {
		return this.insertPurAndDetails();
	}
	
	/**
	 * 提交在线采购订单
	 * @return
	 */
	public String submitOnlinePurAndDetails() {
		return this.insertPurAndDetails();
	}
	
	/**
	 * 提交非在线采购订单
	 * @return
	 */
	public String submitNotOnlinePurAndDetails() {
		return this.insertPurAndDetails();
	}

}
